package com.wf.it.hpdept.service.impl;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.wf.it.common.api.DeptService;
import com.wf.it.common.entity.Dept;
import com.wf.it.common.vo.Result;
import com.wf.it.hpdept.dao.DeptDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptDao dao;

    @Override
    public Result<Map<String,Dept>> findByNames(String[] names) {
        List<Dept> list=dao.findByNameIn(names);
        Map<String,Dept> map=new HashMap<>();
        for(Dept d:list)
            map.put(d.getName(),d);
        return new Result<Map<String,Dept>>(map);
    }

    @Override
    public Result<Dept> findByName(String name) {
        Dept dept=dao.findByName(name);
        return new Result(dept);
    }

    @Override
    public Result<List<Dept>> findAll() {
      List<Dept> list=  dao.findAll();
      return new Result(list);
    }

    @Override
    public Result<Dept> findById(int id) {
        Dept dept=dao.findById(id).get();
        return new Result(dept);
    }

    @Override
    @LcnTransaction
    @Transactional
    public Result add(Dept dept) {
        dao.save(dept);
        return Result.success();
    }
}
